package com.person.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.person.mapper.*;
import com.person.model.dto.DepartmentDto;
import com.person.model.entity.*;
import com.person.model.vo.ComplaintBaseInfoVo;
import com.person.service.ComplaintAcceptanceService;
import com.person.service.ComplaintBaseInfoService;
import com.person.service.ContactsService;
import com.person.service.RegisterAcceptanceService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.HashSet;
import java.util.List;

@Service
public class ComplaintBaseInfoServiceImpl implements ComplaintBaseInfoService {

    @Resource
    private ComplaintAcceptanceService complaintAcceptanceService;

    @Resource
    private ContactsService contactsService;

    @Resource
    private RegisterAcceptanceService registerAcceptanceService;

    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private ComplaintInfoMapper complaintInfoMapper;
    @Resource
    private AgentInfoMapper agentInfoMapper;
    @Resource
    private ComplainantInfoMapper complainantInfoMapper;

    @Override
    public ComplaintBaseInfoVo getComplaintBaseInfoById(Integer id) {
        ComplaintBaseInfoVo complaintBaseInfo = new ComplaintBaseInfoVo();
        ComplaintInfo complaintInfo = complaintInfoMapper.selectById(id);
        BeanUtils.copyProperties(complaintInfo, complaintBaseInfo);
        Department department = departmentMapper.selectById(complaintInfo.getDepartmentId());
        complaintBaseInfo.setDepartmentName(department.getName());
        complaintBaseInfo.setNotRegisteredDesc(department.getNotRegisteredDesc());
        complaintBaseInfo.setNotRegisteredTerm(department.getNotRegisteredTerm());
        complaintBaseInfo.setRegisteredTerm(department.getRegisteredTerm());

        //根据移交部门id 查询移交部门信息
        if ("其他部门移送".equals(complaintInfo.getSource()) || "上级交办".equals(complaintInfo.getSource())) {
            Integer transferDepartmentId = complaintInfo.getTransferDepartmentId();
            Department transferDepartment = departmentMapper.selectById(transferDepartmentId);
            DepartmentDto departmentDto = new DepartmentDto();
            BeanUtils.copyProperties(transferDepartment, departmentDto);
            LambdaQueryWrapper<Contacts> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Contacts::getDepartmentId, transferDepartmentId);
            List<Contacts> contactList = contactsService.list(queryWrapper);
            departmentDto.setContactsList(contactList);
            complaintBaseInfo.setTransferDepartmentDto(departmentDto);
        }

        //封装被投诉人的信息
        LambdaQueryWrapper<AgentInfo> agentInfoWrapper = new LambdaQueryWrapper<>();
        agentInfoWrapper.eq(AgentInfo::getCreditCode, complaintInfo.getCreditCode());
        AgentInfo agentInfo = agentInfoMapper.selectOne(agentInfoWrapper);
        complaintBaseInfo.setAgentInfo(agentInfo);

        //封装投诉人信息
        Integer complaintInfoId = complaintInfo.getComplainantId();
        ComplainantInfo complainantInfo = complainantInfoMapper.selectById(complaintInfoId);
        complaintBaseInfo.setComplainantInfo(complainantInfo);

        //封装投诉信息
        LambdaQueryWrapper<ComplaintAcceptance> complaintWrapper = new LambdaQueryWrapper<>();
        complaintWrapper.eq(ComplaintAcceptance::getComplaintInfoId, id);
        ComplaintAcceptance complaintAcceptance = complaintAcceptanceService.getOne(complaintWrapper);
        if (complaintInfo.getIsAcceptance() == 1) { //如果案件受理了就把负责案件的联系人查到
            String contactsId = complaintAcceptance.getContactsId();
            String[] split = contactsId.split(",");
            //如果基础配置部门联系人的信息变了，就把complaint_acceptance表中联系人的id更新
            ArrayList<Contacts> contacts = new ArrayList<>();
            StringBuilder ids = new StringBuilder();
            for (String contactId : split) {
                Contacts contact = contactsService.getById(Integer.parseInt(contactId));
                if (contact != null) {
                    contacts.add(contact);
                    ids.append(contactId).append(",");
                }
            }
            String idString = ids.deleteCharAt(ids.length() - 1).toString();
            LambdaUpdateWrapper<ComplaintAcceptance> complaintUpdateWrapper = new LambdaUpdateWrapper<>();
            complaintUpdateWrapper.eq(ComplaintAcceptance::getComplaintInfoId, complaintInfoId)
                    .set(ComplaintAcceptance::getContactsId, idString);
            complaintAcceptanceService.update(complaintUpdateWrapper);
            StringBuilder names = new StringBuilder();
            HashSet<String> phoneSet = new HashSet<>();
            for (Contacts contact : contacts) {
                names.append(contact.getName()).append(",");
                phoneSet.add(contact.getPhone());
            }
            String nameString = names.deleteCharAt(names.length() - 1).toString();
            String phoneString = String.join(",", phoneSet);
            complaintBaseInfo.setContactsId(contactsId);
            complaintBaseInfo.setContactPerson(nameString);
            complaintBaseInfo.setContactPhone(phoneString);
        }
        complaintBaseInfo.setIsAcceptance(complaintAcceptance.getIsAcceptance());
        complaintBaseInfo.setAcceptanceDate(complaintAcceptance.getAcceptanceDate());
        complaintBaseInfo.setMediateDate(complaintAcceptance.getMediateDate());
        complaintBaseInfo.setAcceptanceSituation(complaintAcceptance.getSituation());

        //封装举报信息
        LambdaQueryWrapper<RegisterAcceptance> registerWrapper = new LambdaQueryWrapper<>();
        registerWrapper.eq(RegisterAcceptance::getComplaintInfoId, id);
        RegisterAcceptance registerAcceptance = registerAcceptanceService.getOne(registerWrapper);
        complaintBaseInfo.setRegisterAcceptance(registerAcceptance);

        //预处理splitComplaintDesc方便前端填入相应的变量
        String complaintDescSource = complaintBaseInfo.getComplaintDesc();
        String[] splitComplaintDesc = complaintDescSource.split("，");
        String s1 = splitComplaintDesc[0]; //我局接到投诉举报信
        String var1 = s1.substring(4);
        String var2 = complaintDescSource.substring(s1.length() + 5 + agentInfo.getName().length());
        complaintBaseInfo.setComplaintDesc(var1 + "，" + var2);
        return complaintBaseInfo;
    }
}
